﻿/**
 * 首页加载更多
 */
//上拉加载更多
$(document.body).infinite();
var loading = false;  //状态标记
$(document.body).infinite().on("infinite", function() {
	if($(".weui-loadmore").is(":hidden")) return;
	if(loading) return;
	loading = true;
	var offset=$(".pop-itema").length;
	console.log(offset);
	var size=12;
	setTimeout(function() {
		console.time();
		addmore(offset,size);
		console.timeEnd();
	}, 500);   //模拟延迟
});
function addmore(offset,size){
	$.ajax({
		url: 'AddMore',
		data: JSON.stringify({offset: offset,size: size,leibie: leibie,tejia: tejia,sjid:sjid}),
		type: 'post',
		contentType:"application/json;charset=utf-8",
		success: function (data) {
			if(data.length>0){
				$.each(data,function(i,v){
					var a= 
						"<a href='javascript:;' class='pop-itema' spid='"+v.spid+"'>"
						+"<div class='pop_item'>"
						+"<div class='pop_img'>"
						+"<img class='lazy' src='http://www.hnhcyy.com/resource/img/"+v.spid+"/10.jpg' alt='"+v.spmch+"' onerror=\"nofound("+"'"+v.spid+"'"+")\">"
						+"</div>"
						+"<div class='pop_price'>"
						+"<span>"+v.spmch+"</span>";
					if(v.productBal[0].kxshl==0||v.productBal[0].kxshl==null){
						a+="<span style='color:#da0203;display:-webkit-inline-box'>缺货</span>"
					}
					a+="</div>"
						+"</div>"
						+"</a>";
					$(".list_container").append(a);
					initsize=$(".pop-itema:visible").length;
				});
				loading = false;
				/*$("img.lazy").lazyload({
			    	 effect : "fadeIn",
			    });*/
			}else{
				console.log("no more");
				$(".weui-loadmore").hide();
				$(document.body).destroyInfinite()
			}
		}
	});
}
/*var ff = navigator.userAgent.indexOf('Firefox') != -1;
function addmore(offset,size){
	$.ajax({
        url: 'AddMore',
        data: JSON.stringify({offset: offset,size: size,leibie: leibie,tejia: tejia}),
        type: 'post',
        contentType:"application/json;charset=utf-8",
        success: function (data) {
        	if(data.length>0){
        		$.each(data,function(i,v){
        			var a = 
        				"<a href='Shop/Detail?productid="+v.spid+"' class='pop-itema'>"
        				+"<div class='pop_item clearfix'>"
        				+"<div>"
        				//+"<img class='lazy' src='images/product/"+v.spid+"/0.jpg'/>" 
        				+"<img class='lazy' src='http://www.hnhcyy.com/resource/img/"+v.spid+"/10.jpg'>"
        				+"</div>"
        				+"<div class='pop_price'>"
        				+"<div class='clearfix' style='font-size: 12px;line-height: 16px'>"+v.spmch+"</div>"
        				+"</div>"
        				+"</div>"
        				+"</a>"
        			$(".list_container").append(a);
        			initsize=$(".pop-itema:visible").length;
        			if (location.hash.indexOf(a) == -1) location.hash += a;
        		});
        	}else{
        		$(".addmore").testRemind("没有更多了");
        	}
        }
    });
}
var initsize=$(".pop-itema").length;
var size=12;
$(".addmore").on("click",function(){
	var offset=$(".pop-itema").length;
	addmore(offset,size);
})

var offset=$(".pop-itema").length;
 	if (!ff) { //由于firefox回退能保存js动态生成内容，所以不对firefox执行hash判断
        var hash = location.hash;
        if (hash.indexOf('a') != -1) //hash中包含添加时间动作，则执行下操作
        	var leibie=null;
        	var tejia=0;
        	addmore(offset,size);
    }*/